﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using Pcb.Common;

namespace Pcb.Web.tools
{
    /// <summary>
    /// Summary description for registered
    /// </summary>
    public class registered : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            string param = _Request.GetString("param", "");
            switch (param)
            {               
                case "AddUser":             // 普通会员注册
                    AddUser();
                    break;
                case "CheckMobile":         // 检查手机号是否已被注册过了
                    CheckMobile();
                    break;    
                    
            }
        }

        /// <summary>
        /// 普通会员注册
        /// </summary>
        private void AddUser()
        {
            string outmsg = "恭喜您，注册成功！";

            // 验证码
            var code = _Request.GetString("validateCode", "");
            if (code.ToLower() != Pcb.Common._Session.GetSession(DTKeys.SESSION_CODE).ToLower())
            {
                outmsg = "验证码输入不正确！";
            }
            else
            {
                /******* 添加允许操作后台的客户表 *********/
                Pcb.BLL.manager manager = new BLL.manager();
                Pcb.Model.manager managerInfo = new Model.manager();

                //2客户组	3销售人员组	  4财务人员组
                managerInfo.role_id = 2;
                managerInfo.role_type = 2;
                managerInfo.user_name = _Request.GetString("mobile");
                managerInfo.user_pwd = _MD5Encrypt.GetMD5(_Request.GetString("pwd"));
                managerInfo.real_name = _Request.GetString("name");
                managerInfo.telephone = _Request.GetString("tel");                
                managerInfo.email = _Request.GetString("email");
                managerInfo.is_lock = 0;
                managerInfo.add_time = DateTime.Now;
                managerInfo.Mobile = _Request.GetString("mobile");
                managerInfo.Company = _Request.GetString("company");                               
                managerInfo.Qq = _Request.GetString("qq");                
                managerInfo.Fax = _Request.GetString("fax");
                managerInfo.ProviceId = _Request.GetInt("proviceId",0);
                managerInfo.CityId = _Request.GetInt("cityId", 0);
                managerInfo.Address = _Request.GetString("address");
              
                var isOK = manager.Add(managerInfo);

                if (isOK > 0)
                {
                    /******* 添加商城客户表 *********/
                    Pcb.BLL.users user = new BLL.users();
                    Pcb.Model.users userInfo = new Model.users();

                    userInfo.group_id = 1;
                    userInfo.user_name = _Request.GetString("mobile");
                    userInfo.password = _MD5Encrypt.GetMD5(_Request.GetString("pwd"));
                    userInfo.email = _Request.GetString("email");
                    userInfo.nick_name = _Request.GetString("name");
                    //userInfo.avatar =
                    userInfo.sex = "保密";
                    //userInfo.birthday = 
                    userInfo.telphone = _Request.GetString("tel");
                    userInfo.mobile = _Request.GetString("mobile");
                    userInfo.qq = _Request.GetString("qq");
                    userInfo.address = _Request.GetString("address");
                    //userInfo.safe_question = 
                    //userInfo.safe_answer = 
                    userInfo.amount = 0;    //预存款
                    userInfo.point = 0;     //积分
                    userInfo.exp = 0;       //经验值
                    userInfo.is_lock = 0;
                    userInfo.reg_time = DateTime.Now;
                    userInfo.reg_ip = _Utility.GetIp();                  

                    isOK = user.Add(userInfo);
                }

                if (isOK <= 0)
                {
                    outmsg = "注册失败！";
                }
            }

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(outmsg);
            HttpContext.Current.Response.End();
        }

        /// <summary>
        /// 检查手机号是否已被注册过了
        /// </summary>
        private void CheckMobile()
        {
            string outmsg = "1";    //1未被使用过  0已被占用
            string mobile = _Request.GetString("mobile");

            Pcb.BLL.manager manager = new BLL.manager();
            Pcb.Model.manager managerInfo = new Model.manager();
            
            var ds = manager.GetList("mobile = '" + mobile.Trim() + "'");

            if (ds != null)
            {
                if (ds.Tables[0].Rows.Count > 0)
                    outmsg = "0";
            }
 
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(outmsg);
            HttpContext.Current.Response.End();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}